#!/usr/bin/perl -w

#=======================================================================
# access_log-drops
# File ID: 94b18e7a-5d36-11df-9276-90e6ba3022ac
# Finner drops (ikke sukkertøy, men pauser) i Apache-logger.
#
# Character set: UTF-8
# ©opyleft 2004– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License, see end of file for legal stuff.
#=======================================================================

use strict;
use Time::Local;

$| = 1;

use Getopt::Std;
our ($opt_h, $opt_l) =
    (     0,      1);
getopts('hl:') || die("Option error. Use -h for help.\n");

my $VERSION = "0.0";

our $progname = $0;
$progname =~ s#^.*/(.*?)$#$1#;

$opt_h && usage(0);

# 194.248.216.3 - - [13/Aug/2001:16:04:37 +0200] "GET

my %mnd_hash = (
    'Jan'=>0, 'Feb'=>1, 'Mar'=>2, 'Apr'=>3, 'May'=>4,  'Jun'=>5,
    'Jul'=>6, 'Aug'=>7, 'Sep'=>8, 'Oct'=>9, 'Nov'=>10, 'Dec'=>11
);

my $last_sec = 0;

while (<>) {
    if (
        # access_log-regexp {{{
        m!
        \S+ # IP
        \s+
        \S+
        \s+
        \S+
        \s+
        \[
        (\d+) # Dato
        /
        (\S+) # Mnd
        /
        (\d{4}) # År
        :(\d\d):(\d\d):(\d\d) # Klokka
        \s+
        ([+\-]\d\d)(\d\d)
        !x
        # }}}
    ) {
        my ($Day, $Mon, $Year, $Hour, $Min, $Sec, $zone_hour, $zone_min) =
           (  $1,   $2,    $3,    $4,   $5,   $6,         $7,        $8);
        my $num_mon = $mnd_hash{$Mon};
        my $Secs = timegm($Sec, $Min, $Hour, $Day, $num_mon, $Year);
        $Secs -= ($zone_hour*3600); # Vi driter i minuttene for å få opp farta.
        my $Pause = $Secs - $last_sec;
        if ($last_sec && ($Pause >= $opt_l)) {
            $num_mon++; # Å inn i svarteste hælvete hvor jeg hater det inkonsekvente pisset der!!!!!!!!!!!!!!!!
            printf(
                "%04u-%02u-%02uT%02u:%02u:%02uZ %4u %s\n",
                $Year, $num_mon, $Day, $Hour, $Min, $Sec,
                $Pause, sec_to_hms($Pause)
            );
        }
        $last_sec = $Secs;
    } else {
        print(STDERR "Ukjent linje $.\n");
    }
}

sub sec_to_hms {
	# {{{
	my $secs = shift;
	my ($Day, $Hour, $Min, $Sec) = (0, 0, 0, 0);

	$Day = int($secs/86400);
	$secs -= $Day*86400;

	$Hour = int($secs/3600);
	$secs -= $Hour * 3600;

	$Min = int($secs/60);
	$secs -= $Min * 60;

	$Sec = $secs;

	return(($Day ? "$Day:" : "") . sprintf("%02u:%02u:%02u", $Hour, $Min, $Sec));
	# }}}
} # Tidsperiode()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;
    print(<<END);
$progname v$VERSION

Usage: $progname [options] [file [...]]

Scans Apache access logs and prints out time periods with no connections.

Options:

-h    Print this help.
-l x  Show only pauses longer than x seconds.

END
    exit($Retval);
    # }}}
}

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME

access_log-drops — find pauses in Apache logs

=head1 SYNOPSIS

access_log-drops [options] [file [...]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>

Print a brief help summary.

=item B<-l> x

Only list pauses longer than I<x> seconds.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny _AT_ sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm &lt;sunny@sunbase.org&gt;
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
